/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.coding.kata.nantes.countwords.christophe;

import com.google.common.base.Splitter;
import fr.coding.kata.nantes.countwords.commons.KataCountWordsInterface;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

public class KataCountWordsMultiThread implements KataCountWordsInterface {
    private static final String EOL = System.getProperty("line.separator");
    private static final Splitter SPLITTER = Splitter.on(EOL);
    protected ConcurrentLinkedDeque<String> lines;
    protected String word;

    protected AtomicInteger nbWords = new AtomicInteger(0);

    @Override
    public KataCountWordsInterface withText(String anText) {
        Iterable<String> strings = SPLITTER.split(anText);
        lines = new ConcurrentLinkedDeque<String>();
        for (String line : strings) {
            lines.add(line);
        }
        return this;
    }

    @Override
    public KataCountWordsInterface withWord(String anWord) {
        word = anWord;
        return this;
    }

    @Override
    public Integer call() throws Exception {
        int nThreads = 1;
        ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
        Collection<Future<?>> futures = new ArrayList<Future<?>>();
        for (int i = nThreads; i > 0; i--) {
            Future<?> submit = executorService.submit(new Runnable() {
                public void run() {
                    String poll = "";
                    while (poll != null) {
                        poll = lines.poll();
                        Integer delta = countWordInLine(poll);
                        nbWords.getAndAdd(delta);
                    }
                }

                public Integer countWordInLine(String line) {
//                    int count = -1;
//                    int index = -1;
//                    int wordSize = word.length();
//                    do {
//                        index = line.indexOf(word, index + wordSize);
//                        count++;
//                    } while (index != -1);
//                    return count;
                    return StringUtils.countMatches(line, word);
                }
            }, Void.TYPE);

            futures.add(submit);
        }

        for (Future<?> future : futures) {
            future.get();
        }
        return nbWords.get();
    }

}
